package org.seedltd.upms.mapper.detection;

import java.util.List;

import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Update;
import org.seedltd.core.base.BasePage;
import org.seedltd.core.base.IBaseMapper;
import org.seedltd.upms.model.detection.entity.DetectionOrder;
import org.seedltd.upms.model.detection.form.DetectionOrderProductForm;
import org.seedltd.upms.model.detection.form.DetectionOrderSearchForm;
import org.seedltd.upms.model.detection.form.MiniOrderSearchForm;
import org.seedltd.upms.model.detection.vo.DetectionOrderProductVO;
import org.seedltd.upms.model.detection.vo.DetectionOrderVO;

/**
 * 订单管理数据库访问接口
 */
public interface DetectionOrderMapper extends IBaseMapper<DetectionOrder> {

    /**
     * 插入订单和商品的关联关系
     * 
     * @param orderId
     *            订单主键
     * 
     * @param dataList
     *            产品主键
     * @return
     */
    int insertOrderProduct(@Param("orderId") Long orderId,
        @Param("productIds") List<DetectionOrderProductForm> dataList);

    /**
     * 修改商品和订单中间表状态
     * 
     * @param orderId
     *            订单主键
     * @param productId
     *            产品主键
     * @param state
     *            订单检测项目的检测结果
     * @param checkResult
     *            检测项结果
     */
    @Update("UPDATE `detection_order_product` SET `state` = #{state}, `item_name_val` = #{checkResult} WHERE order_id = #{orderId} and product_id = #{productId}")
    int updateOrderProduct(@Param("orderId") Long orderId, @Param("productId") Long productId,
        @Param("state") String state, @Param("checkResult") String checkResult);

    /**
     * 获取到订单的检测项目列表
     * 
     * @param orderId
     *            订单主键
     * @return
     */
    List<DetectionOrderProductVO> listProductByOrderId(@Param("orderId") Long orderId);

    /**
     * 获取二维码绑定的订单主键
     * 
     * @param qrCodeId
     *            二维码主键
     * @return
     */
    @Select("select id from detection_order where qrcode_id = #{qrCodeId}")
    Long findIdByQrCodeId(Long qrCodeId);

    BasePage<DetectionOrderVO> selectBgList(@Param("page") BasePage<DetectionOrderVO> page,
        @Param("ew") DetectionOrderSearchForm searchForm);

    List<DetectionOrderVO> list(@Param("page") BasePage<DetectionOrder> page, @Param("ew") MiniOrderSearchForm form,
        @Param("isSample") boolean isSample);


    List<DetectionOrderVO> listOrderDeliver(Long userId);
}
